package cn.changein.framework.modular.system.controller;

import cn.changein.framework.core.exception.enumeration.BizExceptionEnum;
import cn.changein.framework.core.util.UeditorUtil;
import cn.changein.framework.kernel.model.exception.ServiceException;
import cn.changein.framework.modular.system.model.UeditorFileResult;
import cn.hutool.core.io.resource.ResourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;

/**

 * @date: 2020-02-20
 * @copyright: 广州澄境科技有限公司
 */
@Controller
@RequestMapping("/ueditor")
@Slf4j
public class UeditorController {

    /**
     * 获取ueditor的配置
     *
     * @param response
     */
    @RequestMapping("/config")
    @ResponseBody
    public void getConfigInfo(HttpServletResponse response, HttpServletRequest request) {
        try {
            String action = request.getParameter("action");
            if (action.equals("config")) {
                response.setContentType("application/json;charset=utf-8");
                String json = ResourceUtil.readStr("classpath:ueditor.json", StandardCharsets.UTF_8);
                response.getWriter().write(json);
                response.getWriter().flush();
            } else if (action.equalsIgnoreCase("uploadimage")) {
                request.getRequestDispatcher("/ueditor/imgUpdate").forward(request, response);
            } else if (action.equalsIgnoreCase("uploadvideo")) {
                request.getRequestDispatcher("/ueditor/uploadvideo").forward(request, response);
            } else {
                request.getRequestDispatcher("/ueditor/uploadfile").forward(request, response);
            }
        } catch (Exception e) {
            log.error("ueditor请求失败!", e);
            throw new ServiceException(BizExceptionEnum.UE_CONFIG_ERROR);
        }
    }

    /**
     * 图片上传
     *
     * @param upfile
     * @return
     */
    @RequestMapping(value = "/imgUpdate")
    @ResponseBody
    public UeditorFileResult imgUpdate(@RequestParam(value = "upfile") MultipartFile upfile) {
        return UeditorUtil.uploadFile(upfile, UeditorUtil.FileType.IMG);
    }

    /**
     * ueditor文件上传方法
     *
     * @param upfile
     * @return
     */
    @RequestMapping(value = "/uploadfile", method = RequestMethod.POST)
    @ResponseBody
    public UeditorFileResult uploadFile(@RequestParam(value = "upfile") MultipartFile upfile) {
        return UeditorUtil.uploadFile(upfile, UeditorUtil.FileType.FILE);
    }

    /**
     * 视频上传
     *
     * @param upfile
     * @return
     */
    @RequestMapping(value = "/uploadvideo")
    @ResponseBody
    public UeditorFileResult uploadvideo(@RequestParam(value = "upfile") MultipartFile upfile) {
        return UeditorUtil.uploadFile(upfile, UeditorUtil.FileType.VIDEO);
    }

    /**
     * 通过url请求返回图像的字节流
     *
     * @param fileName
     * @param response
     */
    @RequestMapping("/images/{fileName}")
    public void getImages(@PathVariable("fileName") String fileName, HttpServletResponse response) {
        try {
            UeditorUtil.readFile(fileName, response, UeditorUtil.FileType.IMG, null);
        } catch (Exception e) {
            log.warn("文件不存在:{}", fileName);
        }
    }


    /**
     * 通过url请求返回文件的字节流
     *
     * @param fileName
     * @param orginalName
     * @param response
     */
    @RequestMapping("/file/{fileName}/{orginalName}")
    public void getIco(@PathVariable("fileName") String fileName, @PathVariable("orginalName") String orginalName, HttpServletResponse response) {
        try {
            UeditorUtil.readFile(fileName, response, UeditorUtil.FileType.FILE, orginalName);
        } catch (Exception e) {
            log.warn("文件不存在:{}", fileName);
        }
    }


    /**
     * 通过url请求返回图像的字节流
     *
     * @param fileName
     * @param response
     */
    @RequestMapping("/video/{fileName}")
    public void getVideo(@PathVariable("fileName") String fileName, HttpServletResponse response) {
        try {
            UeditorUtil.readFile(fileName, response, UeditorUtil.FileType.VIDEO, null);
        } catch (Exception e) {
            log.warn("文件不存在:{}", fileName);
        }
    }
}

